
const WebSocketServer = require('ws').Server,
    wss = new WebSocketServer({port:9000});

var clientMap = new Object();
var i = 0;

wss.on('connection',function (ws) {
    console.log(ws+'上线');
    ws.name = ++i;
    clientMap[ws.name] = ws;
    ws.on('message',function (message) {
        broadcast(message,ws);
    })
    ws.on('close',function () {
        console.log('离开');
    })
})

var broadcast= function (msg,ws){
    for(var key in clientMap){
        clientMap[key].send('第'+ws.name+'个说: '+msg);
    }
}